import os
import sys
import time
from Daemon import Daemon


class MyTestDaemon(Daemon):
    def run(self):
        sys.stdout.write('Daemon started with pid {%s}\n' % os.getpid())
        while True:
            sys.stdout.write('Daemon Alive! {%s}\n' % time.ctime())
            sys.stdout.flush()

            time.sleep(5)


if __name__ == '__main__':
    PIDFILE = '/tmp/daemon-sysinfo.pid'
    LOG = '/tmp/daemon-sysinfo.log'
    daemon = MyTestDaemon(pidfile=PIDFILE, stdout=LOG, stderr=LOG)

    if len(sys.argv) != 2:
        print('Usage: {} [start|stop]'.format(sys.argv[0]))
        raise SystemExit(1)

    if 'start' == sys.argv[1]:
        daemon.start()
    elif 'stop' == sys.argv[1]:
        daemon.stop()
    elif 'restart' == sys.argv[1]:
        daemon.restart()
    else:
        print('Unknown command {!r}'.format(sys.argv[1]))
        raise SystemExit(1)
